package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
// */
////@RequestMapping("/depts")
//@RestController
//public class DeptController {
//
//    @Autowired
//    private DeptService deptService;
//
//    @GetMapping("/depts")
//    public Result list(){
//        System.out.println("查询全部部门数据");
//        List<Dept> deptList = deptService.findAll();
//        return Result.success(deptList);
//    }
//
//
//    /**
//     //     * 根据ID删除部门 - 简单参数接收: 方式一 (HttpServletRequest)
//     //     */
////    @DeleteMapping("/depts")
////    public Result delete(HttpServletRequest request){
////        String idStr = request.getParameter("id");
////        int id = Integer.parseInt(idStr);
////
////        System.out.println("根据id删除部门:"+id);
////        return Result.success();
////    }
////    //该参数必须传递,或者修改一下required
////    @DeleteMapping("/depts")
////    public Result delete(@RequestParam(value = "id",required = false) Integer deptId){
////        System.out.println("根据id删除部门:" + deptId);
////        return Result.success();
////    }
//    //如果请求参数名与形参变量名相同，直接定义方法形参即可接收。（省略@RequestParam）
//    @DeleteMapping("/depts")
//    public Result delete(Integer id){
//        System.out.println("根据ID删除部门: " + id);
//        deptService.deleteById(id);
//        return Result.success();
//    }
//
//    @PostMapping("/depts")
//    public Result add(@RequestBody Dept dept){
//        System.out.println("新增部门:" + dept);
//        deptService.add(dept);
//        return Result.success();
//    }
//
//    @GetMapping("/depts/{id}")
//    public Result getById(@PathVariable Integer id){
//        System.out.println("根据ID查询, id=" + id);
//        Dept dept = deptService.getById(id);
//        return Result.success(dept);
//    }
//
//    @PutMapping("/depts")
//    public Result update(@RequestBody Dept dept){
//        System.out.println("修改部门信息: " + dept);
//        deptService.update(dept);
//        return Result.success();
//    }
//
//}


/**
 * 部门管理控制器
 */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 查询部门列表
     */
    //@RequestMapping(value = "/depts", method = RequestMethod.GET)
    @GetMapping
    public Result list(){
        //System.out.println("查询部门列表");
        log.info("查询部门列表");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    /**
     * 根据id删除部门 - delete http://localhost:8080/depts?id=1
     */
    @Log
    @DeleteMapping
    public Result delete(Integer id){
        //System.out.println("根据id删除部门, id=" + id);
        log.info("根据id删除部门, id: {}" , id);
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 新增部门 - POST http://localhost:8080/depts   请求参数：{"name":"研发部"}
     */
    @Log
    @PostMapping
    public Result save(@RequestBody Dept dept){
        //System.out.println("新增部门, dept=" + dept);
        log.info("新增部门, dept: {}" , dept);
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据ID查询 - GET http://localhost:8080/depts/1
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        //System.out.println("根据ID查询, id=" + id);
        log.info("根据ID查询, id: {}" , id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门 - PUT http://localhost:8080/depts  请求参数：{"id":1,"name":"研发部"}
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        //System.out.println("修改部门, dept=" + dept);
        log.info("修改部门, dept: {}" , dept);
        deptService.update(dept);
        return Result.success();
    }
}